package com.smartcruise.task;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.smartcruise.R;
import com.smartcruise.base.BaseTitleActivity;
import com.smartcruise.base.TitleBuilder;
import com.smartcruise.data.cruiseData.CruiseContract;
import com.smartcruise.data.cruiseData.CruiseData;
import com.smartcruise.data.cruiseData.NodeTaskListBean;
import com.smartcruise.report.reportRecord.ReportRecordActivity;
import com.smartcruise.util.IntentUtils;
import com.smartcruise.util.Key;
import com.smartcruise.util.ListUtils;
import com.smartcruise.util.TimeUtils;
import com.smartcruise.util.Utils;
import com.smartcruise.util.schedulerUtil.CronTransfer;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.realm.RealmChangeListener;
import io.realm.RealmList;

/**
 * Created by FengChaoQun
 * on 2018/1/25 0025
 * 任务总览
 */

public class TaskDetailsActivity extends BaseTitleActivity implements TaskDetailsContract.View {

    @BindView(R.id.taskName)
    TextView taskName;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.period)
    TextView period;
    @BindView(R.id.path)
    TextView path;
    @BindView(R.id.planUseTime)
    TextView planUseTime;
    @BindView(R.id.usedTime)
    TextView usedTime;
    @BindView(R.id.auditPeople)
    TextView auditPeople;
    @BindView(R.id.reportRecord)
    LinearLayout reportRecord;
    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.bottomButton)
    Button bottomButton;
    @BindView(R.id.scrollView)
    ScrollView scrollView;
    @BindView(R.id.planStartTime)
    TextView planStartTime;
    @BindView(R.id.realStartTime)
    TextView realStartTime;

    private CruiseData cruiseData;
    private RealmList<NodeTaskListBean> nodeTaskList;
    private TaskDetailsContract.Presenter mPresenter;
    private Calendar calendar;

    public static void start(Context context, String taskId, Calendar calendar) {
        Intent intent = new Intent(context, TaskDetailsActivity.class);
        intent.putExtra(IntentUtils.DATA, taskId);
        intent.putExtra(IntentUtils.DATE, calendar);
        context.startActivity(intent);
    }

    @Override
    protected void onDestroy() {
        if (nodeTaskList != null) {
            nodeTaskList.removeAllChangeListeners();
        }
        super.onDestroy();
    }

    @Override
    protected void init() {

        String id = getIntent().getStringExtra(IntentUtils.DATA);
        calendar = (Calendar) getIntent().getSerializableExtra(IntentUtils.DATE);
        if (TextUtils.isEmpty(id)) {
            showToast(getString(R.string.needId));
            finish();
            return;
        }
        cruiseData = realm.where(CruiseData.class).equalTo(Key.id, id).findFirst();
        if (cruiseData == null) {
            showToast(getString(R.string.noContent));
            finish();
            return;
        }

        initView();

        mPresenter = new TaskDetailsPresenterImp(this, cruiseData);
        mPresenter.start();

        nodeTaskList = cruiseData.getNodeTaskList();
        if (nodeTaskList != null) {
            nodeTaskList.addChangeListener(new RealmChangeListener<RealmList<NodeTaskListBean>>() {
                @Override
                public void onChange(RealmList<NodeTaskListBean> nodeTaskListBeans) {
                    initView();
                }
            });
        }

        if (ListUtils.isHaveContent(nodeTaskList)) {
            NodeAdapter adapter = new NodeAdapter(nodeTaskList, getActivity(), cruiseData);
            listView.setAdapter(adapter);
            ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = Utils.px(70 * cruiseData.getNodeTaskList().size());
            listView.setLayoutParams(layoutParams);
        }

        scrollView.smoothScrollTo(0, 0);
    }

    private void initView() {
        taskName.setText(cruiseData.getName());
        type.setText(cruiseData.getType());
        period.setVisibility(View.GONE);
        if (cruiseData.getPatrollingRouteEntity() != null) {
            path.setText(cruiseData.getPatrollingRouteEntity().getName());
        } else {
            path.setText("无");
        }
        planUseTime.setText(cruiseData.getCostTime() + "小时");
        usedTime.setText("0");
        auditPeople.setText(cruiseData.getAuditor().getUserName());

        if (cruiseData.getShouldRunDate() > 0) {
            planStartTime.setText(TimeUtils.getSimpleTimeString(cruiseData.getShouldRunDate()));
        } else {//计算出时间
            if (calendar != null) {
                List<Date> dates = CronTransfer.computeFireTimesBetween(cruiseData.getCronExpression(),
                        TimeUtils.getDayBeginTime(calendar), TimeUtils.getDayEndTime(calendar));
                if (ListUtils.isHaveContent(dates)) {
                    planStartTime.setText(TimeUtils.getSimpleTimeString(dates.get(0).getTime()));
                }
            }
        }

        if (cruiseData.getActualRunDate() > 0) {
            realStartTime.setText(TimeUtils.getSimpleTimeString(cruiseData.getActualRunDate()));
            if (cruiseData.getActualStopDate() > 0) {
                usedTime.setText(TimeUtils.getUsedTime(cruiseData.getActualStopDate() - cruiseData.getActualRunDate()));
            } else {
                if (CruiseContract.isStarted(cruiseData)) {
                    long useTime = TimeUtils.currentTime() - cruiseData.getActualRunDate();
                    usedTime.setText(TimeUtils.getUsedTime(useTime));
                }
            }
        }
        if (CruiseContract.isCanModify(cruiseData)) {
            bottomButton.setVisibility(View.VISIBLE);
        } else {
            bottomButton.setVisibility(View.GONE);
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_container_task_detail;
    }

    @Override
    protected TitleBuilder getTitleBuilder() {
        return TitleBuilder.create(true, getString(R.string.taskView));
    }

    @OnClick(R.id.reportRecord)
    public void onReportRecordClicked() {
        ReportRecordActivity.start(getActivity(), cruiseData.getPatrollingTaskId());
    }

    @OnClick(R.id.bottomButton)
    public void onBottomButtonClicked() {
        mPresenter.clickOnUploadAll();
    }

}
